# -*- coding: utf-8 -*-

# Bliotecas padrões
from django.contrib.auth import authenticate, logout, login as authlogin
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.template import Context, loader, RequestContext
from django.core.urlresolvers import reverse
from django.contrib.admin.models import User
from django.contrib.auth.decorators import permission_required

# Bibliotecas externas

# Bibliotecas das apps
from myproject.settings import MEDIA_URL, MEDIA_ROOT
from myproject.aula.forms import *
from cadastro.extras import *

# Models
from myproject.aula.models import *

############################################
# Implementa as funcoes do model Curso     #
############################################
@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def curso(request):
  f = CursoModelForm()
  return render_to_response('aula/curso.html', {'cursos' : Curso.objects.filter(professor__user__id=request.user.id)}
                            ,context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def cursoProfessor(request):
    qs = Curso.objects.filter(professor__user__id=request.user.id)
    return render_to_response('aula/cursoProfessor.html', {'cursos' : qs}
                              ,context_instance=RequestContext(request))
    
@permission_required('cadastro.per_administrador')  
def adicionar_curso(request):
    if request.POST:
      f = CursoModelForm(request.POST, request.FILES,)
      if f.is_valid():
        c = f.save()
        return HttpResponseRedirect(reverse('aula.views.curso'))
    else:
      f = CursoModelForm()
    return render_to_response('aula/curso/adicionar_curso.html', {'form':f,},context_instance=RequestContext(request))

@permission_required('cadastro.per_administrador')
def mostra_dados_curso(request, codigo):
  curso = get_object_or_404(Curso, pk=codigo)
  codigo = curso.id
  f = CursoModelForm(instance=curso)
  return render_to_response('aula/curso/curso_dados.html', {'form':f, 'codigo':codigo},context_instance=RequestContext(request))   

@permission_required('cadastro.per_administrador')
def alterar_curso(request, codigo):
    curso = get_object_or_404(Curso, pk=codigo)
    f = CursoModelForm(request.POST, request.FILES, instance=curso)
    if f.is_valid():
        curso = f.save()
        return HttpResponseRedirect(reverse('aula.views.curso'))
    return render_to_response('aula/curso/adicionar_curso.html', {'form':f,},context_instance=RequestContext(request))

@permission_required('cadastro.per_administrador')
def apagar_curso(request, codigo):
    curso = get_object_or_404(Curso, pk=codigo)
    curso.delete()
    return HttpResponseRedirect(reverse('aula.views.curso'))

############################################
# Implementa as funcoes do model Aula      #
############################################
@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def aula(request,cod_curso):
    return render_to_response('aula/aula.html', {'aulas' : Aula.objects.filter(curso__id=cod_curso),'cod_curso':cod_curso}
                                              , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def adicionar_aula(request,cod_curso):
    if request.POST:
        f = AulaModelForm(request.POST, request.FILES,cod_curso=cod_curso)
        if f.is_valid():
            c = f.save()
            return HttpResponseRedirect(reverse('aula.views.curso'))
    else:
        f = AulaModelForm(cod_curso=cod_curso)
    return render_to_response('aula/aula/adicionar_aula.html', {'form':f,},context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def mostra_dados_aula(request, codigo):
  aula = get_object_or_404(Aula, pk=codigo)
  codigo = aula.id
  f = AulaModelForm(instance=aula)
  return render_to_response('aula/aula/aula_dados.html', {'cod_curso':aula.curso.id,'form':f, 'codigo':codigo},context_instance=RequestContext(request))   

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def alterar_aula(request, codigo):
    aula = get_object_or_404(Aula, pk=codigo)
    f = AulaModelForm(request.POST, request.FILES, instance=aula)
    if f.is_valid():
        aula = f.save()
        return HttpResponseRedirect(reverse('aula.views.curso'))
    return render_to_response('aula/aula/adicionar_aula.html', {'form':f,},context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def apagar_aula(request, codigo):
    aula = get_object_or_404(Aula, pk=codigo)
    aula.delete()
    return HttpResponseRedirect(reverse('aula.views.curso'))

##################################################
# Implementa as funcoes do model Arquivo da aula #
##################################################
@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def arquivoAula(request,cod_aula):
    return render_to_response('aula/arquivoAula.html'   , { 'arquivosAula' : ArquivoAula.objects.filter(aula__id=cod_aula)
                                                        , 'cod_aula':cod_aula}
                                                        , context_instance=RequestContext(request))

@permission_required('cadastro.per_aluno')
def arquivoAulaAluno(request,cod_aula):
    return render_to_response('aula/arquivoAulaAluno.html'\
                                                        , { 'arquivosAula' : ArquivoAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}\
                                                        , context_instance=RequestContext(request))


@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def adicionar_arquivoAula(request,cod_aula):
    if request.POST:
        f = ArquivoAulaModelForm(request.POST, request.FILES,cod_aula)
        if f.is_valid():
            c = f.save()
            return render_to_response('aula/arquivoAula.html'   , { 'arquivosAula' : ArquivoAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}
                                                                , context_instance=RequestContext(request))
    else:
        f = ArquivoAulaModelForm(cod_aula=cod_aula)
    return render_to_response(  'aula/arquivoAula/adicionar_arquivoAula.html', {'form':f,}
                                , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def mostra_dados_arquivoAula(request, codigo):
    aula = get_object_or_404(ArquivoAula, pk=codigo)
    codigo = aula.id
    f = ArquivoAulaModelForm(instance=aula)
    return render_to_response('aula/arquivoAula/arquivoAula_dados.html'
                              ,{'form':f, 'codigo':codigo}
                              ,context_instance=RequestContext(request))   

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def alterar_arquivoAula(request, codigo):
    aula = get_object_or_404(ArquivoAula, pk=codigo)
    f = ArquivoAulaModelForm(request.POST, request.FILES, instance=aula)
    if f.is_valid():
        aula = f.save()
        return HttpResponseRedirect(reverse('aula.views.curso'))
    return render_to_response(  'aula/arquivoAula/arquivoAula_dados.html'
                                , {'form':f, 'codigo':aula.id}
                                , context_instance=RequestContext(request))    

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def apagar_arquivoAula(request, codigo):
    aula = get_object_or_404(ArquivoAula, pk=codigo)
    aula.delete()
    return HttpResponseRedirect(reverse('aula.views.curso'))
    
#################################################
# Implementa as funcoes do model Imagem da aula #
#################################################
@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def imagemAula(request,cod_aula):  
    teste = ImagemAula.objects.filter(aula__id=cod_aula)
    return render_to_response('aula/imagemAula.html'    , { 'imagensAula' : ImagemAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}
                                                        , context_instance=RequestContext(request))

@permission_required('cadastro.per_aluno')
def imagemAulaAluno(request,cod_aula):    
    return render_to_response('aula/imagemAulaAluno.html'   , { 'imagensAula' : ImagemAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}
                                                            , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])  
def adicionar_imagemAula(request,cod_aula):
    if request.POST:
        f = ImagemAulaModelForm(request.POST, request.FILES, cod_aula=cod_aula)
        if f.is_valid():
            c = f.save()
            print c.id
            return render_to_response('aula/imagemAula.html'    , { 'imagensAula' : ImagemAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}
                                                                , context_instance=RequestContext(request))
    else:
        f = ImagemAulaModelForm(cod_aula=cod_aula)
    return render_to_response(  'aula/imagemAula/adicionar_imagemAula.html', {'form':f,}
                                , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def mostra_dados_imagemAula(request, codigo):
    aula = get_object_or_404(ImagemAula, pk=codigo)
    codigo = aula.id
    f = ImagemAulaModelForm(instance=aula)
    return render_to_response(  'aula/imagemAula/imagemAula_dados.html'
                                , {'form':f,'codigo':codigo}
                                , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def alterar_imagemAula(request, codigo):
    aula = get_object_or_404(ImagemAula, pk=codigo)
    f = ImagemAulaModelForm(request.POST, request.FILES, instance=aula)
    if f.is_valid():
        aula = f.save()
        return HttpResponseRedirect(reverse('aula.views.curso'))
    return render_to_response(  'aula/imagemAula/imagemAula_dados.html', {'form':f,}
                                , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def apagar_imagemAula(request, codigo):
    aula = get_object_or_404(ImagemAula, pk=codigo)
    aula.delete()
    return HttpResponseRedirect(reverse('aula.views.curso'))

################################################
# Implementa as funcoes do model Video da aula #
################################################
@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def videoAula(request,cod_aula):
    return render_to_response('aula/videoAula.html'   , { 'videosAula' : VideoAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}
                                                        , context_instance=RequestContext(request))

@permission_required('cadastro.per_aluno')
def videoAulaAluno(request,cod_aula):
    videos = VideoAula.objects.filter(aula__id=cod_aula)
    videolist = ''
    for video in videos:
        videolist = videolist +u"'"+ str(video.get_absolute_url())+u"',"
    videolist = videolist[:-1]
    print videolist
    return render_to_response('aula/videoAulaAluno.html'\
                                                        , { 'videoAula' : VideoAula.objects.filter(aula__id=cod_aula),
                                                            'cod_aula':cod_aula,
                                                            'videolist':videolist}\
                                                        , context_instance=RequestContext(request))


@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def adicionar_videoAula(request,cod_aula):
    if request.POST:
        f = VideoAulaModelForm(request.POST, request.FILES,cod_aula=cod_aula)
        if f.is_valid():
            c = f.save()
            return render_to_response('aula/videoAula.html'   , { 'videosAula' : VideoAula.objects.filter(aula__id=cod_aula),'cod_aula':cod_aula}
                                                              , context_instance=RequestContext(request))
    else:
      f = VideoAulaModelForm(cod_aula=cod_aula)
    print f
    return render_to_response(  'aula/videoAula/adicionar_videoAula.html', {'form':f,}
                                , context_instance=RequestContext(request))

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def mostra_dados_videoAula(request, codigo):
    video = get_object_or_404(VideoAula, pk=codigo)
    codigo = video.id
    f = VideoAulaModelForm(instance=video)
    return render_to_response(  'aula/videoAula/videoAula_dados.html'
                              , {'form':f, 'codigo':codigo},context_instance=RequestContext(request))   

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def alterar_videoAula(request, codigo):
    video = get_object_or_404(VideoAula, pk=codigo)
    f = VideoAulaModelForm(request.POST, request.FILES, instance=video)
    if f.is_valid():
        aula = f.save()
        return HttpResponseRedirect(reverse('aula.views.curso'))
    return render_to_response(  'aula/videoAula/adicionar_videoAula.html', {'form':f,}
                                , context_instance=RequestContext(request))    

@permission_required_tcc(['cadastro.per_professor','cadastro.per_administrador'])
def apagar_videoAula(request, codigo):
    aula = get_object_or_404(VideoAula, pk=codigo)
    aula.delete()
    return HttpResponseRedirect(reverse('aula.views.curso'))